<?php
	define('CONFIG_FILE',true);
	include '../config.php';
	
	define('DB_FILE',true);
	require_once '../Tool/DB/SqlDBManager.class.php';
	require_once '../Tool/Common/PasswordHash.php';
	
	session_start();
	if (empty($_SESSION["userType"])) {
		$url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
	}

	if($_SESSION["userType"]=="Therapy Assistant"){
        $url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
    }
    
	$Username = $_REQUEST['InsertUsername'];
	$UserType = $_REQUEST['InsertUserType'];
	$Password = $_REQUEST['InsertPassword'];
	$StaffName = $_REQUEST['InsertStaffName'];
	$ContactNumber = $_REQUEST['InsertContactNumber'];
	$Email = $_REQUEST['InsertEmail'];
	$centreId = $_REQUEST['InsertCentreId'];

	$passHashed = create_hash($Password);
	//$pass = substr($Password, 0, 7); 

	$sqlDBManager = new SqlDBManager();

	if($centreId!= 0){
		$sql = "insert into user(Username,Password,UserType,StaffName,CentreId,ContactNumber,Email) values(?,?,?,?,?,?,?)";
		$parameters = array($Username,$passHashed,$UserType,$StaffName,$centreId,$ContactNumber,$Email);
		$res=$sqlDBManager->execute_dml($sql,$parameters);
		
		//close connection
  		$sqlDBManager->close_connect();

		if ($res>0){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>'Create new user failed due to database error.'));
		}
	}
	else{
		$sql = "insert into user(Username,Password,UserType,StaffName,CentreId,ContactNumber,Email) values(?,?,?,?,NULL,?,?)";
		$parameters=array($Username,$passHashed,$UserType,$StaffName,$ContactNumber,$Email);
		$res=$sqlDBManager->execute_dml($sql);
		
		//close connection
  		$sqlDBManager->close_connect();

		if ($res>0){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>'Create new user failed due to database error.'));
		}
	}
?>